package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AlbumInfoMapper extends BaseMapper<AlbumInfo> {
    /**
     * 分页查询用户专辑列表
     * @param page   mybatis-plus会自动将limit追加到sql末尾  原理：拦截器
     * @param albumInfoQuery
     * @return
     */
    IPage<AlbumListVo> selectUserAlbumPage(Page<AlbumListVo> page, @Param("vo") AlbumInfoQuery albumInfoQuery);

    /**
     * 根据id查寻统计信息
     * @param albumId
     * @return
     */
    @Select("select stat.album_id,\n" +
            "       max(if(stat_type = '0401', stat_num, 0)) playStatNum,\n" +
            "       max(if(stat_type = '0402', stat_num, 0)) subscribeStatNum,\n" +
            "       max(if(stat_type = '0403', stat_num, 0)) buyStatNum,\n" +
            "       max(if(stat_type = '0404', stat_num, 0)) commentStatNum\n" +
            "from album_stat stat\n" +
            "where stat.album_id = #{albumId} and stat.is_deleted = 0\n" +
            "group by stat.album_id")
    AlbumStatVo selectAlbumStatVo(@Param("albumId") Long albumId);
    /**
     * 更新专辑统计信息
     * @param count
     * @param albumId
     * @param statType
     */
    @Update("update album_stat set stat_num = album_stat.stat_num + #{count} where stat_type = #{statType} and is_deleted = 0 and album_id = #{albumId}")
    void updateAlbumStat(@Param("count") Integer count, @Param("albumId") Long albumId, @Param("statType") String statType);
}
